package com.glink.common.util;

import com.glink.common.contants.ErrorCode;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.vo.ResponseMessage;
import com.glink.common.vo.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Objects;


/**
 * 处理微服务调用返回值
 */
public class FeignResponseUtil {

    public static <T> T getEntity(ResponseResult<T> responseResult)throws GeneralSimpleException {
        if(Objects.isNull(responseResult)){
            throw new GeneralSimpleException("F300001");
        }
        if (StringUtils.equals(responseResult.getStatus(), ErrorCode.REQ_SUCCESS.getValue())) {
            return responseResult.getData();
        } else {
            throw new GeneralSimpleException("F300010", responseResult.getStatus(), responseResult.getMessage());
        }
    }

    public static <T> T getEntityMsg(ResponseEntity<ResponseMessage<T>> responseEntity)throws GeneralSimpleException {
        if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
            if (StringUtils.equals(responseEntity.getBody().getStatus(), ErrorCode.REQ_SUCCESS.getValue())) {
                return responseEntity.getBody().getResult();
            } else {
                throw new GeneralSimpleException("F300010", responseEntity.getBody().getStatus(), responseEntity.getBody().getMessage());
            }
        } else {
            throw new GeneralSimpleException(responseEntity.getStatusCode().toString());
        }
    }
}
